﻿using System.Collections.Generic;
using System.Linq;
using Castle.DynamicProxy;
using System.Reflection;

namespace TianYuan.Core.Domain.Uow.Interception
{
    /// <summary>
    /// EfUnitOfWork拦截器，加上[UnitOfWork]则会自动提交事务
    /// 如果有[NoUnitOfWork]属性则不提交
    /// </summary>
    public abstract class UnitOfWorkInterceptorabstract : IInterceptor
    {
        
        //internal readonly IUnitOfWork _unitOfWork;

        //public UnitOfWorkInterceptorabstract(IUnitOfWork unitOfWork)
        //{
        //    _unitOfWork = unitOfWork;
        //}

        public abstract void Intercept(IInvocation invocation);
        //{
        //    //var disableFiltersAttr = AttributeHelper.GetDisableFiltersAttributeOrNull(invocation.MethodInvocationTarget);
        //    //if (disableFiltersAttr != null)
        //    //{
        //    //    List<string> filterNames = disableFiltersAttr.FilterNames.Select(filterName => filterName.ToString()).ToList();
        //    //    _unitOfWork.DisableFilters(filterNames.ToArray());
        //    //}
        //    //MethodInfo method = invocation.Method;
        //    invocation.Proceed();//先执行方法，下面再自动提交事务，如果有NoUnitOfWork属性则不提交
        //    //if (Attribute.IsDefined(method, typeof (NoUnitOfWorkAttribute)))
        //    //{
        //    //    return;
        //    //}
        //    //if (invocation.Method.IsDefined(typeof(NoUnitOfWorkAttribute), true))
        //    //{
        //    //    return;
        //    //}
        //    var unitOfWorkAttr = AttributeHelper.GetNoUnitOfWorkAttributeOrNull(invocation.MethodInvocationTarget);
        //    if (unitOfWorkAttr != null)
        //    {
        //        return;
        //    }
        //    _unitOfWork.Commit();
        //}

        
    }
}